/*
 * Fighter.java
 *
 * Created on 31 October 2006, 22:17
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package digitrix.fightclub.game;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

/**
 *
 * @author Josh
 */
public class Fighter extends Actor {
    
    protected int _playerID; // used to associate loaded sprite with what images are loaded
    protected boolean _reverseMovement = false; 
    
    /** Creates a new instance of Fighter */
    public Fighter( Image img, int width, int height, int playerID )  {
        super( img, width, height );
        
        _playerID = playerID;         
    }  

    public void handleInput(int keyState) {
        if( (keyState & GameManager.FIRE_PRESSED ) != 0 ){
            int stateID = _currentState.getStateID(); 
            
            if( (stateID == STATE_STANDING || stateID == STATE_WALKING) && stateID != STATE_DYING ){                
                setState( getFightingStateByInput( keyState ) ); 
            }
        }
        else if( (keyState & GameManager.LEFT_PRESSED) != 0 ){
            if( !_reverseMovement )
                moveLeft(); 
            else
                moveRight(); 
        }
        else if( (keyState & GameManager.RIGHT_PRESSED) != 0 ){
            if( !_reverseMovement )
                moveRight(); 
            else
                moveLeft(); 
        }
    }   
    
    public void setState( ActorState newState ){
        if( newState == null || newState == getCurrentState() )
            return; 
        
        newState.activateState();  
    }
    
    public void stateComplete( ActorState state ) {
        
        if( state.getStateID() == STATE_DYING ){
            // TODO: Notify GameManager that this player is dead 
            GameManager.getInstance().setState( GameManager.GS_FIGHTOVER );  
        }
        else
            setState( STATE_STANDING ); 
    }
    
    public void tick( long elapsedTime ){
       super.tick( elapsedTime );
    }
    
    //<editor-fold defaultstate="collapsed" desc="mutators">
    
    public int getPlayerID(){
        return _playerID; 
    }
    
    public void setPlayerID( int value ){
        _playerID = value; 
    }
    
    public boolean isCritical(){
        if( _health <= Math.abs(_maxHealth/3) )
            return true;
        else
            return false; 
    }
    
    public void setReverseMovemet( boolean rm ){
        _reverseMovement = rm; 
    }
    
    public boolean getReverseMovement(){
        return _reverseMovement; 
    }
    
    //</editor-fold>
    
}
